<?php

namespace App\Http\Api\Controllers;

use App\Exceptions\InvalidRequestException;

use App\Http\Controllers\Controller;
use App\Http\Models\BliMsg;
use App\Http\Models\BliUper;
use App\Http\Models\Qingyun;
use App\Lib\Naipan\Naipan;
use App\Lib\PHPExcel\ExcelHandle;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Lib\DingDingNotice\House;


class MediaController extends Controller
{
    public function text(Request $request)
    {

        $text = $request->input('text', null);
        $result = '';
        if ($text) {
            $result = $text;
//            $result = (new Naipan())->textTran($text);
        }

        return res('ok', $result);

    }

    //关键词列表 八斗会
    public function bdSearch(Request $request)
    {

        $keyword = $request->input('keyword');
        $auth    = $request->input('auth') ?? '';

        $url = "https://search.daorc.com/main_welcome2.action?uuu=7c7ffa93fd6d45bbbdbc7ed064e7b07b?set=getContentReason";

        $header = [
            "Referer:https://drbl.daorc.com/picturelibrary_getParagraphView.action?standard=1&min_h=500&min_w=500&title=&describe=&url=&type=&itemId=&bname=&typename=%E6%A8%A1%E5%9D%97&isopen=1&temp=1594964414578",
            "Authorizationr:$auth"
        ];

        $data = "contentKeyWords=".$keyword;

        $res = curlPost($url,$data,$header);
        return res('ok',$res);
    }

    //b站up主采集
    public function bliUper(Request $request)
    {

        $attributes = $request->all();
        $mid = $attributes['mid'];

       $flag = BliUper::where('mid',$mid)->exists();
       if($flag){
           unset($attributes['mid']);
           $result = BliUper::where('mid',$mid)->update($attributes);
       }else{
           $result = BliUper::create($attributes);
       }

        return res('ok', $result);

    }

    //青云计划数据采集
    public function qingyun(Request $request)
    {
        $data = $request->input("data");

        if(empty($data)){
            return false;
        }
        if(!is_array($data)){
            $data = json_decode($data,true);
        }
        foreach ($data as $item){
            $flag = Qingyun::where('id',$item['id'])->exists();
            if($flag){
                $result = Qingyun::where('id',$item['id'])->update($item);
            }else{
                $result = Qingyun::create($item);
            }
        }


        return res('ok', $result);

    }

    public function msg(Request $request){
		header('Content-Type: text/html;charset=utf-8');
		header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
		header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
		header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
		header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段
        $user = $request->input('user') ?? '';
        $phone =  $request->input('phone');
        $content =  $request->input('content') ?? '';
        $platform =  $request->input('platform') ?? '';
        if(!$phone){
            throw new InvalidRequestException('请填写电话号码');
        }
        if(strlen($phone) != 11){
            throw new InvalidRequestException('请输入正确电话号码');
        }

        BliMsg::create([
            'user' => $user,
            'phone' => $phone,
            'content' => $content,
            'platform' => $platform
        ]);
		$pubDate = date('Y-m-d H:i:s');
		$token = "a30373eb5efb87a70ab13dc7fe38f142721c10dc706a389b6d8d79f56c409ef4";
        //发送消息通知
		$house = new House($platform,$user,$phone,$pubDate,$token);

		$house->sendMarkdownMessage();
        return res('添加成功');

    }

    public function expFdcData(){
        $title = '联系方式';
        $result = BliMsg::all();
        ExcelHandle::factory('Msg', $title, $result->toArray())->export();
    }

}
